package com.shen.langchain4j.controller;

import com.shen.langchain4j.service.ChatAssistant;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.DocumentTransformer;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.parser.apache.tika.ApacheTikaDocumentParser;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

@Slf4j
@RestController
@RequestMapping("easyRag")
public class ChatEasyRagController {
    @Autowired
    private InMemoryEmbeddingStore<TextSegment> inMemoryEmbeddingStore;
    @Autowired
    private ChatAssistant chatAssistant;

    @GetMapping("/chatWithRag")
    public String chatWithRag() throws FileNotFoundException {
//        Document document1 = FileSystemDocumentLoader.loadDocument("F:\\test\\通义千问大模型异常状态码说明.docx");
        FileInputStream fileInputStream = new FileInputStream("F:\\test\\通义千问大模型异常状态码说明.docx");
        Document document = new ApacheTikaDocumentParser().parse(fileInputStream);
        EmbeddingStoreIngestor.ingest(document, inMemoryEmbeddingStore);
        String answer = chatAssistant.chat("错误代码为duplicate_custom_id是什么含义");
        log.info("answer: {}", answer);
        return answer;
    }
}
